package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProblemReplyListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ProblemBackOriginal;
import com.isunland.managesystem.entity.ProblemReplyOriginal;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by lenovo on 2016/11/17.
 */
public class ProblemReplyListFragment extends BaseListFragment {
  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.ProblemReplyListFragment.extra_value";
  public static final int ADD = 0;
  private ProblemReplyListAdapter mAdapter;
  private ProblemBackOriginal.ProblemBackContent mcontent;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.reply_list);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mcontent = (ProblemBackOriginal.ProblemBackContent) getArguments().getSerializable(
        ProblemBackListFragment.EXTRA_VALUE);
    super.onCreate(savedInstanceState);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  /**
   * 获取url
   */
  @Override public String getUrl() {
    return ApiConst.URL_PROBLEM_BACK_REPLYLIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("mainId", mcontent.getId());
    params.put("orderField", "reg_date");
    params.put("orderSeq", "desc");

    return params;
  }

  /**
   * 解析响应
   */
  @Override public void resolveResponse(String json) {
    ProblemReplyOriginal original = new Gson().fromJson(json, ProblemReplyOriginal.class);
    ArrayList<ProblemReplyOriginal.ProblemReplyContent> rows =
        (ArrayList<ProblemReplyOriginal.ProblemReplyContent>) original.getRows();
    setAdapter(rows);
  }

  private void setAdapter(ArrayList<ProblemReplyOriginal.ProblemReplyContent> rows) {
    if (rows == null) {
      rows = new ArrayList<>();
    }
    if (mAdapter == null) {
      mAdapter = new ProblemReplyListAdapter(getActivity(), rows);
    }
    setListAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ProblemReplyOriginal.ProblemReplyContent content = mAdapter.getItem(position - 1);
    Intent i = new Intent(getActivity(), ProblemReplyDetailActivity.class);
    i.putExtra(EXTRA_VALUE, content);
    startActivityForResult(i, ADD);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode != Activity.RESULT_OK) {
      return;
    }
    if (resultCode == ADD) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static ProblemReplyListFragment newInstance(
      ProblemBackOriginal.ProblemBackContent content) {

    Bundle args = new Bundle();
    args.putSerializable(ProblemBackListFragment.EXTRA_VALUE, content);
    ProblemReplyListFragment fragment = new ProblemReplyListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
